package com.spring.sso.config.security.authentication.impl;

import com.spring.sso.config.common.AuthenticationConfig;
import com.spring.sso.config.common.DefaultUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author : pangfuzhong
 * @description
 * @date : 2021/9/15 11:15
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService, UserDetailsPasswordService {

    @Autowired
    private AuthenticationConfig authenticationConfig;

    @Override
    public UserDetails updatePassword(UserDetails userDetails, String s) {
        return null;
    }

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {

        // 校验同一用户名是否重复登陆

        // 2、获取用户
        DefaultUserDetails defaultUserDetails = CommonService.loadUserByUsername(this.authenticationConfig, userName);
        if(Objects.isNull(defaultUserDetails)) {
            throw new UsernameNotFoundException("未查询到当前登陆用户[" + userName + "]");
        }

        return defaultUserDetails;
    }
}
